
$(function () {
    group_table_init();
});

/**初始化分组列表*/
function group_table_init() {
    $.post(ctx+"myCustomer/selectGroupList",{},function (r) {
        $("#mc-group-table").empty();
        if(r.code===0){
           var data = r.data;
           if(data.length>0){
               $.each(data,function(index,val){
                   var tr ='<tr uuid="'+val.cGroupUuid+'"><td class="text-center"><a href="javascript:void(0);" id="upid'+val.cGroupUuid+'">'+val.cGroupName+'</a></td><td class="text-center">' +
                       '<a href="#" onclick="sortGoupDown(this)" ><i class="fa fa-long-arrow-down"></i></a>&emsp;'+
                       '<a href="#" onclick="sortGoupUp(this)" ><i class="fa fa-long-arrow-up"></i></a>&emsp;'+
                       '<a href="#" onclick="deleteGroup(this)"><i class="fa fa-trash-o p-l-5"></i></a></td></tr>';
                   $("#mc-group-table").append(tr);
                   init_firstTab(val.cGroupUuid);
               });
           }else{
               $("#mc-group-table").append("<tr><td class='text-center' colspan='3'>当前还没有创建客户分组</td></tr>");
           }
        }
    });
}

/**删除分组*/
function deleteGroup(btn) {
    var uuid = $(btn).parent().parent().attr("uuid");
    $JuXue.confirm({
        text: "确定删除选中的分组？",
        confirmButtonText: "确定删除"
    }, function() {
        $.post(ctx + 'myCustomer/deleteGroup', { "cGroupUuid": uuid }, function(r) {
            if (r.code === 0) {
                $JuXue.n_success(r.msg);
                $(btn).parent().parent().remove();
                $("#mg-left-group").find("li").each(function () {
                   var liuuid = $(this).attr("uuid");
                    if (uuid == liuuid) $(this).remove();
                });
            } else {
                $JuXue.n_danger(r.msg);
            }
        });
    });
}

/**排序下移动操作*/
function sortGoupDown(btn) {
    var btnTr = $(btn).parent().parent();
    var trnext = btnTr.next();
    if (trnext.length <= 0){$JuXue.n_warning("不能再下移");return;}
    //获取移动的 点击的id
    var uuidbefore = btnTr.attr("uuid");
    //获取将要移动的id
    var uuidafter = trnext.attr("uuid");
    if (uuidbefore != undefined && uuidafter!= undefined){
        var url = ctx+"myCustomer/sortGroup";
        $.get(url,{beforeUp:uuidbefore,afterUp:uuidafter},function (r) {
           if(r.code===0){
               $JuXue.n_success(r.msg);
               group_table_init();init_left_groupList()
           }else {
               $JuXue.n_danger(r.msg);
           }
        });
    }else{
        $JuXue.n_warning("获取移动数据失败");
    }
}
/**上移东**/
function sortGoupUp(btn) {
    var btnTr = $(btn).parent().parent();
    var trprev = btnTr.prev();
    if (trprev.length <= 0){$JuXue.n_warning("不能再上移");return;}
    //获取移动的 点击的id
    var uuidbefore = btnTr.attr("uuid");
    //获取将要移动的id
    var uuidafter = trprev.attr("uuid");
    if (uuidbefore != undefined && uuidafter!= undefined){
        var url = ctx+"myCustomer/sortGroup";
        $.get(url,{beforeUp:uuidbefore,afterUp:uuidafter},function (r) {
            if(r.code===0){
                $JuXue.n_success(r.msg);
                group_table_init();init_left_groupList()
            }else {
                $JuXue.n_danger(r.msg);
            }
        });
    }else{
        $JuXue.n_warning("获取移动数据失败");
    }
}

/**修改*/
function init_firstTab(btn) {
    $("#upid" + btn + "").editable({
        url: function (parame) {
            $.post(ctx + "myCustomer/updateGroupName", {cGroupUuid: btn, cGroupName: parame.value}, function (r) {
                if (r.code === 0) {
                    $JuXue.n_success(r.msg);
                } else {
                    $JuXue.n_danger(r.msg);
                }
            });
        },
        pk: '',
        defaultValue: "",
        title: "修改分组名称",
        validate: function (value) { //字段验证
            if (!$.trim(value)) {
                return '不能为空';
            }
        }
    });
}